package com.lp.security.config;

import com.aliyun.green20220302.Client;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.teaopenapi.models.Config;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @program: backend
 * @description:
 * @author: Ke.Song
 * @since: 2024-05-29 20:57:50
 */
@Configuration
public class AliConfig {

    @Value("${ali.oss.accessKeyId}")
    private String ossAccessKeyId;

    @Value("${ali.oss.accessKeySecret}")
    private String ossAccessKeySecret;

    @Value("${oss.endpoint}")
    private String endpoint;

    @Value("${ali.sts.accessKeyId}")
    private String accessKeyId;

    @Value("${ali.sts.accessKeySecret}")
    private String accessKeySecret;

    @Value("${ali.sts.regionId}")
    private String regionId;

    @Value("${ali.sts.endpoint}")
    private String stsEndpoint;

    @Value("${ali.green.endpoint}")
    private String greenEndpoint;

    @Bean
    public OSS buildOss() {
        return new OSSClientBuilder()
                .build(endpoint, ossAccessKeyId, ossAccessKeySecret);
    }

    @Bean
    public DefaultAcsClient buildAcsClient() {
        // 添加endpoint。适用于Java SDK 3.12.0及以上版本。
        DefaultProfile.addEndpoint(regionId, "Sts", stsEndpoint);
        // 构造default profile。
        IClientProfile profile = DefaultProfile.getProfile(regionId, accessKeyId, accessKeySecret);
        return new DefaultAcsClient(profile);
    }

    @Bean
    public Client buildGreenClient() throws Exception {
        Config config = new Config();
        config.setAccessKeyId(accessKeyId);
        config.setAccessKeySecret(accessKeySecret);
        config.setRegionId(regionId);
        config.setEndpoint(greenEndpoint);
        //连接时超时时间，单位毫秒（ms）。
        config.setReadTimeout(6000);
        //读取时超时时间，单位毫秒（ms）。
        config.setConnectTimeout(3000);
        return new Client(config);
    }

}
